IF NOT EXISTS (SELECT * FROM msdb.dbo.sysjobs WHERE [name] = '<%= @jobname %>')
BEGIN
USE msdb ;

DECLARE @job_name nvarchar(max)
SET @job_name = '<%= @jobname %>';

DECLARE @ScheduleName nvarchar(max)
SET @ScheduleName = '<%= @schedulename %>';

DECLARE @DatabasesName nvarchar(max)
SET @DatabasesName = '<%= @databases %>';

DECLARE @fragmentationlow nvarchar(max)
SET @fragmentationlow = '<%= @fragmentationlow %>';

DECLARE @fragmentationmedium nvarchar(max)
SET @fragmentationmedium = '<%= @fragmentationmedium %>';

DECLARE @fragmentationhigh nvarchar(max)
SET @fragmentationhigh = '<%= @fragmentationhigh %>';

DECLARE @fragmentationlevel1 nvarchar(max)
SET @fragmentationlevel1 = '<%= @fragmentationlevel1 %>';

DECLARE @fragmentationlevel2 nvarchar(max)
SET @fragmentationlevel2 = '<%= @fragmentationlevel2 %>';

DECLARE @scheduletime INT
SET @scheduletime = '<%= @scheduletime %>';

DECLARE @freq_type nvarchar(max)
SET @freq_type = '<%= @freq_type_code %>';

DECLARE @freq_interval nvarchar(max)
SET @freq_interval = '<%= @freq_interval_code %>';


DECLARE @TokenServer nvarchar(max)
SET @TokenServer = '$' + '(ESCAPE_SQUOTE(SRVR))';

DECLARE @JobCommand01 nvarchar(max)
SET @JobCommand01 = N'sqlcmd -E -S ' + @TokenServer + ' -d master -Q "EXECUTE dbo.IndexOptimize @Databases = '''+@DatabasesName+''', @fragmentationlow = '+@fragmentationlow+', @fragmentationmedium = '''+@fragmentationmedium+''', @fragmentationhigh = '''+@fragmentationhigh+''', @fragmentationlevel1 = '''+@fragmentationlevel1+''', @fragmentationlevel2 = '''+@fragmentationlevel2+''' " -b';

	EXEC dbo.sp_add_job
		@job_name = @job_name,
		@description = 'Job Created by PUPPET', 
		@category_name = 'Database Maintenance';

	EXEC sp_add_jobstep
		@job_name = @job_name,
		@step_name = @job_name,
		@subsystem = 'CMDEXEC',
		@command = @JobCommand01;

	EXEC dbo.sp_add_schedule
		@schedule_name = @ScheduleName,
		@freq_type = @freq_type,
		@freq_interval = @freq_interval,
		@active_start_time = @scheduletime ;

	EXEC sp_attach_schedule
	   @job_name = @job_name,
	   @schedule_name = @ScheduleName;

	EXEC dbo.sp_add_jobserver
		@job_name = @job_name;
END